home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 2000 October / Software of the Month - Ultimate Collection Shareware 277.iso / pc / PROGRAMS / UTILITY / WINLINUX / DATA1.CAB / programs_-_kernel_source / LIB / STRING.C < prev    next >
C/C++ Source or Header  |  1999-09-17  |  5KB  |  342 lines

  1. /*
  2.  *  linux/lib/string.c
  3.  *
  4.  *  Copyright (C) 1991, 1992  Linus Torvalds
  5.  */
  6.  
  7. /*
  8.  * stupid library routines.. The optimized versions should generally be found
  9.  * as inline code in <asm-xx/string.h>
  10.  *
  11.  * These are buggy as well..
  12.  */
  13.  
  14. #include <linux/types.h>
  15. #include <linux/string.h>
  16. #include <linux/ctype.h>
  17.  
  18. #ifndef __HAVE_ARCH_STRNICMP
  19. int strnicmp(const char *s1, const char *s2, size_t len)
  20. {
  21.     /* Yes, Virginia, it had better be unsigned */
  22.     unsigned char c1, c2;
  23.  
  24.     c1 = 0;    c2 = 0;
  25.     if (len) {
  26.         do {
  27.             c1 = *s1; c2 = *s2;
  28.             s1++; s2++;
  29.             if (!c1)
  30.                 break;
  31.             if (!c2)
  32.                 break;
  33.             if (c1 == c2)
  34.                 continue;
  35.             c1 = tolower(c1);
  36.             c2 = tolower(c2);
  37.             if (c1 != c2)
  38.                 break;
  39.         } while (--len);
  40.     }
  41.     return (int)c1 - (int)c2;
  42. }
  43. #endif
  44.  
  45. char * ___strtok = NULL;
  46.  
  47. #ifndef __HAVE_ARCH_STRCPY
  48. char * strcpy(char * dest,const char *src)
  49. {
  50.     char *tmp = dest;
  51.  
  52.     while ((*dest++ = *src++) != '\0')
  53.         /* nothing */;
  54.     return tmp;
  55. }
  56. #endif
  57.  
  58. #ifndef __HAVE_ARCH_STRNCPY
  59. char * strncpy(char * dest,const char *src,size_t count)
  60. {
  61.     char *tmp = dest;
  62.  
  63.     while (count-- && (*dest++ = *src++) != '\0')
  64.         /* nothing */;
  65.  
  66.     return tmp;
  67. }
  68. #endif
  69.  
  70. #ifndef __HAVE_ARCH_STRCAT
  71. char * strcat(char * dest, const char * src)
  72. {
  73.     char *tmp = dest;
  74.  
  75.     while (*dest)
  76.         dest++;
  77.     while ((*dest++ = *src++) != '\0')
  78.         ;
  79.  
  80.     return tmp;
  81. }
  82. #endif
  83.  
  84. #ifndef __HAVE_ARCH_STRNCAT
  85. char * strncat(char *dest, const char *src, size_t count)
  86. {
  87.     char *tmp = dest;
  88.  
  89.     if (count) {
  90.         while (*dest)
  91.             dest++;
  92.         while ((*dest++ = *src++)) {
  93.             if (--count == 0) {
  94.                 *dest = '\0';
  95.                 break;
  96.             }
  97.         }
  98.     }
  99.  
  100.     return tmp;
  101. }
  102. #endif
  103.  
  104. #ifndef __HAVE_ARCH_STRCMP
  105. int strcmp(const char * cs,const char * ct)
  106. {
  107.     register signed char __res;
  108.  
  109.     while (1) {
  110.         if ((__res = *cs - *ct++) != 0 || !*cs++)
  111.             break;
  112.     }
  113.  
  114.     return __res;
  115. }
  116. #endif
  117.  
  118. #ifndef __HAVE_ARCH_STRNCMP
  119. int strncmp(const char * cs,const char * ct,size_t count)
  120. {
  121.     register signed char __res = 0;
  122.  
  123.     while (count) {
  124.         if ((__res = *cs - *ct++) != 0 || !*cs++)
  125.             break;
  126.         count--;
  127.     }
  128.  
  129.     return __res;
  130. }
  131. #endif
  132.  
  133. #ifndef __HAVE_ARCH_STRCHR
  134. char * strchr(const char * s, int c)
  135. {
  136.     for(; *s != (char) c; ++s)
  137.         if (*s == '\0')
  138.             return NULL;
  139.     return (char *) s;
  140. }
  141. #endif
  142.  
  143. #ifndef __HAVE_ARCH_STRRCHR
  144. char * strrchr(const char * s, int c)
  145. {
  146.        const char *p = s + strlen(s);
  147.        do {
  148.            if (*p == (char)c)
  149.                return (char *)p;
  150.        } while (--p >= s);
  151.        return NULL;
  152. }
  153. #endif
  154.  
  155. #ifndef __HAVE_ARCH_STRLEN
  156. size_t strlen(const char * s)
  157. {
  158.     const char *sc;
  159.  
  160.     for (sc = s; *sc != '\0'; ++sc)
  161.         /* nothing */;
  162.     return sc - s;
  163. }
  164. #endif
  165.  
  166. #ifndef __HAVE_ARCH_STRNLEN
  167. size_t strnlen(const char * s, size_t count)
  168. {
  169.     const char *sc;
  170.  
  171.     for (sc = s; count-- && *sc != '\0'; ++sc)
  172.         /* nothing */;
  173.     return sc - s;
  174. }
  175. #endif
  176.  
  177. #ifndef __HAVE_ARCH_STRSPN
  178. size_t strspn(const char *s, const char *accept)
  179. {
  180.     const char *p;
  181.     const char *a;
  182.     size_t count = 0;
  183.  
  184.     for (p = s; *p != '\0'; ++p) {
  185.         for (a = accept; *a != '\0'; ++a) {
  186.             if (*p == *a)
  187.                 break;
  188.         }
  189.         if (*a == '\0')
  190.             return count;
  191.         ++count;
  192.     }
  193.  
  194.     return count;
  195. }
  196. #endif
  197.  
  198. #ifndef __HAVE_ARCH_STRPBRK
  199. char * strpbrk(const char * cs,const char * ct)
  200. {
  201.     const char *sc1,*sc2;
  202.  
  203.     for( sc1 = cs; *sc1 != '\0'; ++sc1) {
  204.         for( sc2 = ct; *sc2 != '\0'; ++sc2) {
  205.             if (*sc1 == *sc2)
  206.                 return (char *) sc1;
  207.         }
  208.     }
  209.     return NULL;
  210. }
  211. #endif
  212.  
  213. #ifndef __HAVE_ARCH_STRTOK
  214. char * strtok(char * s,const char * ct)
  215. {
  216.     char *sbegin, *send;
  217.  
  218.     sbegin  = s ? s : ___strtok;
  219.     if (!sbegin) {
  220.         return NULL;
  221.     }
  222.     sbegin += strspn(sbegin,ct);
  223.     if (*sbegin == '\0') {
  224.         ___strtok = NULL;
  225.         return( NULL );
  226.     }
  227.     send = strpbrk( sbegin, ct);
  228.     if (send && *send != '\0')
  229.         *send++ = '\0';
  230.     ___strtok = send;
  231.     return (sbegin);
  232. }
  233. #endif
  234.  
  235. #ifndef __HAVE_ARCH_MEMSET
  236. void * memset(void * s,char c,size_t count)
  237. {
  238.     char *xs = (char *) s;
  239.  
  240.     while (count--)
  241.         *xs++ = c;
  242.  
  243.     return s;
  244. }
  245. #endif
  246.  
  247. #ifndef __HAVE_ARCH_BCOPY
  248. char * bcopy(const char * src, char * dest, int count)
  249. {
  250.     char *tmp = dest;
  251.  
  252.     while (count--)
  253.         *tmp++ = *src++;
  254.  
  255.     return dest;
  256. }
  257. #endif
  258.  
  259. #ifndef __HAVE_ARCH_MEMCPY
  260. void * memcpy(void * dest,const void *src,size_t count)
  261. {
  262.     char *tmp = (char *) dest, *s = (char *) src;
  263.  
  264.     while (count--)
  265.         *tmp++ = *s++;
  266.  
  267.     return dest;
  268. }
  269. #endif
  270.  
  271. #ifndef __HAVE_ARCH_MEMMOVE
  272. void * memmove(void * dest,const void *src,size_t count)
  273. {
  274.     char *tmp, *s;
  275.  
  276.     if (dest <= src) {
  277.         tmp = (char *) dest;
  278.         s = (char *) src;
  279.         while (count--)
  280.             *tmp++ = *s++;
  281.         }
  282.     else {
  283.         tmp = (char *) dest + count;
  284.         s = (char *) src + count;
  285.         while (count--)
  286.             *--tmp = *--s;
  287.         }
  288.  
  289.     return dest;
  290. }
  291. #endif
  292.  
  293. #ifndef __HAVE_ARCH_MEMCMP
  294. int memcmp(const void * cs,const void * ct,size_t count)
  295. {
  296.     const unsigned char *su1, *su2;
  297.     signed char res = 0;
  298.  
  299.     for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
  300.         if ((res = *su1 - *su2) != 0)
  301.             break;
  302.     return res;
  303. }
  304. #endif
  305.  
  306. /*
  307.  * find the first occurrence of byte 'c', or 1 past the area if none
  308.  */
  309. #ifndef __HAVE_ARCH_MEMSCAN
  310. void * memscan(void * addr, int c, size_t size)
  311. {
  312.     unsigned char * p = (unsigned char *) addr;
  313.  
  314.     while (size) {
  315.         if (*p == c)
  316.             return (void *) p;
  317.         p++;
  318.         size--;
  319.     }
  320.       return (void *) p;
  321. }
  322. #endif
  323.  
  324. #ifndef __HAVE_ARCH_STRSTR
  325. char * strstr(const char * s1,const char * s2)
  326. {
  327.     int l1, l2;
  328.  
  329.     l2 = strlen(s2);
  330.     if (!l2)
  331.         return (char *) s1;
  332.     l1 = strlen(s1);
  333.     while (l1 >= l2) {
  334.         l1--;
  335.         if (!memcmp(s1,s2,l2))
  336.             return (char *) s1;
  337.         s1++;
  338.     }
  339.     return NULL;
  340. }
  341. #endif
  342.